package com.shuoyue.ycgk.appcatch;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.ahammertest.ycgk.jpush.JPushHelper;
import com.google.gson.Gson;
import com.shuoyue.appdepends.commonutils.XLog;
import com.shuoyue.ycgk.entity.UserInfo;


/**
 * SharedPreferences封装
 *
 * @author Forty'7 2019.6.18
 */
public class SPUtils {


    public static String getPrefString(Context context, String key,
                                       final String defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getString(key, defaultValue);
    }

    public static void setPrefString(Context context, final String key,
                                     final String value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putString(key, value).commit();
    }

    public static boolean getPrefBoolean(Context context, final String key,
                                         final boolean defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getBoolean(key, defaultValue);
    }

    public static boolean hasKey(Context context, final String key) {
        return PreferenceManager.getDefaultSharedPreferences(context).contains(
                key);
    }

    public static void setPrefBoolean(Context context, final String key,
                                      final boolean value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putBoolean(key, value).commit();
    }

    public static void setPrefInt(Context context, final String key,
                                  final int value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putInt(key, value).commit();
    }

    public static int getPrefInt(Context context, final String key,
                                 final int defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getInt(key, defaultValue);
    }

    public static void setPrefFloat(Context context, final String key,
                                    final float value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putFloat(key, value).commit();
    }

    public static float getPrefFloat(Context context, final String key,
                                     final float defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getFloat(key, defaultValue);
    }

    public static void setSettingLong(Context context, final String key,
                                      final long value) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        settings.edit().putLong(key, value).commit();
    }

    public static long getPrefLong(Context context, final String key,
                                   final long defaultValue) {
        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(context);
        return settings.getLong(key, defaultValue);
    }

    public static void clearPreference(Context context,
                                       final SharedPreferences p) {
        final Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }

    static final String LAST_SELECT_MODULE = "last_select_module";

    /**
     * 用户偏好类型
     *
     * @param context
     * @return
     */
    public static String getLastSelectMoudle(Context context) {
        return getPrefString(context, LAST_SELECT_MODULE, "事业单位");
    }

    public static void setLastSelectMoudle(Context context, String name) {
        setPrefString(context, LAST_SELECT_MODULE, name);
    }


    /**
     * 更新登录账号
     *
     * @param context
     * @return
     */
    public static String LAST_PHONE = "last_phone";//记录最后一次登录的手机号

    public static void updataLoginUserName(Context context, String username) {
        if (!TextUtils.isEmpty(username)) {
            setPrefString(context, LAST_PHONE, username);
            XLog.d("SPUtils", "更新登录账号成功 " + getPrefString(context, LAST_PHONE, ""));
        } else {
            XLog.d("SPUtils", "更新登录账号失败");
        }
    }

    /**
     * 获取登录账号
     *
     * @param context
     * @return
     */
    public static String getLoginUserName(Context context) {
        return getPrefString(context, LAST_PHONE, "");
    }


    /**
     * 更新登录密码
     *
     * @param context
     * @return
     */
    public static String LAST_PWD = "last_password";//记录最后一次登录的手机号

    public static void updateUserPwd(Context context, String pwd) {
        if (!TextUtils.isEmpty(pwd)) {
            setPrefString(context, LAST_PWD, pwd);
            XLog.d("SPUtils", "更新登录账号成功 " + getPrefString(context, LAST_PWD, ""));
        } else {
            XLog.d("SPUtils", "更新登录账号失败");
        }
    }

    public static void clearUserPassword(Context context) {
        setPrefString(context, LAST_PWD, "");
    }

    /**
     * 获取登录密码
     *
     * @param context
     * @return
     */
    public static String getLastLoginPwd(Context context) {
        return getPrefString(context, LAST_PWD, "");
    }

    /**
     * 清除用户信息
     *
     * @param context
     */
    public static String LOGIN_USER_BEAN = "login_user_bean";//用户信息

    public static void clearUserInfo(Context context) {
        setPrefString(context, LOGIN_USER_BEAN, "");
    }

    /**
     * 保存用户信息
     *
     * @param context
     * @param bean
     */
    public static void updateUserInfo(Context context, UserInfo bean) {
        if (bean != null) {
            JPushHelper.getInstance().setAlias(context.getApplicationContext(), bean.getPhone());
            String userJson = new Gson().toJson(bean);
            setPrefString(context, LOGIN_USER_BEAN, userJson);
        } else {
        }
    }

    /**
     * 获取用户信息
     */
    public static UserInfo getUserInfo(Context context) {
        String userInfoJson = getPrefString(context, LOGIN_USER_BEAN, "");
        if (!TextUtils.isEmpty(userInfoJson)) {
            XLog.d("SPUtils", "获取用户信息成功 = " + userInfoJson);
            return new Gson().fromJson(userInfoJson, UserInfo.class);
        } else {
            XLog.d("SPUtils", "获取用户信息失败");
            return null;
        }
    }
}

